package com.fy56.platform.logistics.interceptor;

import com.fy56.platform.logistics.dbutil.DataSourceTypeManager;
import com.fy56.platform.logistics.dbutil.LogisticsDataSources;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author li
 * @date 2018-03-08 13:36
 */
@Aspect    // for aop
@Component // for auto scan
@Order(0)  // execute before @Transactional
public class DataSourceInterceptor {
    @Pointcut("execution(public * com.fy56.platform.logistics.service.wx..*.*(..))")
    public void dataSourceSlave(){};

    @Before("dataSourceSlave()")
    public void before(JoinPoint jp) {
        DataSourceTypeManager.set(LogisticsDataSources.SLAVE);
    }

    @Pointcut("execution(public * com.fy56.platform.logistics.service.common..*.*(..)) || execution(public * com.fy56.platform.logistics.service.web..*.*(..))")
    public void dataSourceMaster(){};

    @Before("dataSourceMaster()")
    public void beforeMaster(JoinPoint jp) {
        DataSourceTypeManager.set(LogisticsDataSources.MASTER);
    }

}
